package com.communication.service;

import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.communication.json.CommunicationEntity;
import com.communication.json.JsonResult;
import com.entity.Item;
import com.entity.Measuringpoint;
import com.entity.Meter;
import com.entity.Sysdic;
import com.file.service.MeasuringpointService;
import com.file.service.MeterService;
import com.file.service.TerminalService;
import com.interfaces.ItemDao;
import com.interfaces.MeterDao;
import com.interfaces.SysdicDao;
import com.protocolterminal.ProtocolTerminalService;
import com.protocolterminal.base.Seq;

@Component
public class CommunicationTerminal {

	@Autowired
	private TerminalService terminalservice;
	@Autowired
	private ItemDao itemDao;
	@Autowired
	private MeasuringpointService measuringpointService;
	@Autowired
	private SysdicDao sysdicDao;
	@Autowired
	private MeterService meterService;
	@Autowired
	private MeterDao meterDao;
	
	public String Enframe(JsonResult<CommunicationEntity> jsonResult) {

		// 查询item
		Item item = itemDao.findOne(Integer.valueOf(jsonResult.getData().getIdentification()));

		// 获取信息
		String terminalprotocol = terminalservice
				.findTerminalprotocolByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));
		String terminaladdress = terminalservice
				.findTerminaladdressByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));

		String das = "0000";
		if (item.getAfn().equalsIgnoreCase("04") && item.getDt().equalsIgnoreCase("0201")) {
			// 设置档案
			String data = "";

			String[] DA = jsonResult.getData().getData().split("-");
			data += Integer.toHexString(DA.length) + "-";
			for (String string : DA) {

				Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(
						Integer.valueOf(jsonResult.getData().getTerminalID()), Integer.valueOf(string));

				Sysdic sysdic = sysdicDao.findBySysdictypeAndSysdicvalue("0011",
						meterService.findMeterprotocolByMeterid(Integer.valueOf(string)));

				Meter meter = meterDao.findOne(Integer.valueOf(string));
				String collectoraddress = "000000000000";
				if (measuringpoint.getCollectorid() != null) {

				}

				data += measuringpoint.getMeasuringpointnumber() + "-" + measuringpoint.getMeasuringpointordernumber()
						+ "-" + measuringpoint.getMeasuringpointbaud() + "-" + measuringpoint.getMeasuringpointport()
						+ "-" + sysdic.getSysdicremake() + "-" + meter.getMeteraddress() + "-"
						+ meter.getMeterpassword() + "-4-" + measuringpoint.getMeasuringpointdec() + "-"
						+ measuringpoint.getMeasuringpointint() + "-" + collectoraddress + "-"
						+ measuringpoint.getMeasuringpointbigint() + "-" + measuringpoint.getMeasuringpointsmallint()
						+ "-";
			}
			jsonResult.getData().setData(data.substring(0, data.length() - 1));
		} else if (item.getAfn().equalsIgnoreCase("0A") && item.getDt().equalsIgnoreCase("0201")) {
			// 读取
			String data = "";

			String[] DA = jsonResult.getData().getData().split("-");
			for (String string : DA) {
				Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(
						Integer.valueOf(jsonResult.getData().getTerminalID()), Integer.valueOf(string));
				data += measuringpoint.getMeasuringpointnumber() + "-";
			}
			jsonResult.getData().setData(data.substring(0, data.length() - 1));
		} else {
			Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(
					Integer.valueOf(jsonResult.getData().getTerminalID()),
					Integer.valueOf(jsonResult.getData().getMeterID()));
			if (!item.getDa().equals("0000")) {
				das = measuringpoint.getMeasuringpointnumber();
			}
		}

		// 组帧
		ProtocolTerminalService protocolTerminalService = new ProtocolTerminalService();
		String sentframe = protocolTerminalService.Encode(Integer.valueOf(terminalprotocol), item.getAfn(),
				new Seq(0, 1, 1, 1, 0).toString(), Arrays.asList(das.split("-")),
				Arrays.asList(item.getDt().split("-")), Arrays.asList(jsonResult.getData().getData().split(";")),
				"00000000000000000000000000000000", 1, item.getPRM(), terminaladdress);

		return sentframe;
	}

	public JsonResult<CommunicationEntity> DeFrame(JsonResult<CommunicationEntity> jsonResult, String sentdata,
			String returndata) {
		JsonResult<CommunicationEntity> json = jsonResult;

		String terminalprotocol = terminalservice
				.findTerminalprotocolByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));

		// 比对帧
		ProtocolTerminalService protocolTerminalService = new ProtocolTerminalService();
		String decodedata = protocolTerminalService.Decode(Integer.valueOf(terminalprotocol), sentdata, returndata);

		if (decodedata != null) {
			CommunicationEntity communicationEntity = new CommunicationEntity();
			communicationEntity.setData(decodedata);
			json.setData(communicationEntity);
		} else {
			json.setMessage(decodedata);
		}

		return json;
	}

}
